package com.finger.service.impl;

import com.finger.dao.UserDao;
import com.finger.dao.WordDao;
import com.finger.pojo.*;
import com.finger.service.WordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

@Service
public class WordServiceImpl implements WordService {
    @Autowired
    private WordDao wordDao;
    @Autowired
    private UserDao userDao;

    private long getTime;

    @Override
    public Word getWord(User user) {
        //先把所有单词数据都拿过来
        List<Word> allWords = wordDao.getAllWords(user);
        List<UserLearnedWord> allLearnedWords = wordDao.getAllLearnedWords(user);
        Instant instant = Instant.now(); // 获取当前时间的Instant对象
        getTime = instant.toEpochMilli(); // 将Instant对象转换为时间戳
        ArrayList list = new ArrayList();
        if (allLearnedWords.size() < allWords.size()) {
            //还没学完一遍
            for (Word word : allWords) {
                Boolean flag = true;
                for (UserLearnedWord learnedWord : allLearnedWords) {
                    //当前单词已经学过一遍了，学过之后当前单词会执行下面逻辑，如果没学过不会执行到flag=false
                    if (word.getId().equals(learnedWord.getWid())) {
                        //跳出内层循环
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    //这个单词还没学过
                    return word;
                }
            }
        } else {
            //至少学完一遍
            list.add("小白");             //0
            list.add("还需努力");         //1
            list.add("很棒");             //2
            list.add("四级中的指尖幽灵");  //3
            //知道了当前已经刷过几遍了
            int minWordsLearnCount = wordDao.getMinWordsLearnCount(user.getId());
            Boolean updateA = userDao.updateUserAchievement(user.getId(), list.get(minWordsLearnCount).toString());
            if (updateA) {
                user.setAchievement(list.get(minWordsLearnCount).toString());
            }
            int count = 0;
            //获取该用户当前是刷的第几遍了
            count = list.indexOf(user.getAchievement());
            for (UserLearnedWord learnedWord : allLearnedWords) {
                //当前单词已经学过count遍了，学过count遍当前单词会执行下面逻辑，如果没学count次不会执行到flag=false
                if (learnedWord.getCount().equals(count)) {
                    //跳出内层循环
                    return wordDao.getWordByWid(learnedWord.getWid());
                }
            }
        }

        //idea认为if语句可能执行不到，然后导致方法可能没有正常的返回值
        //这里加上一下就好了，idea还是不够智能啊
        return null;
    }

    @Override
    //用户提交一条单词信息
    public Boolean sendWord(Word word, int uid) throws NullPointerException {
        //先查询这个单词学习过没有
        Integer count = null;
        try {
            count = wordDao.getByUidAndWid(uid, word.getId());
        } catch (Exception e) {

        } finally {
            Instant instant = Instant.now(); // 获取当前时间的Instant对象
            long timestamp = instant.toEpochMilli(); // 将Instant对象转换为时间戳
            UserLearnedWord learnedWord = new UserLearnedWord();
            learnedWord.setUid(uid);
            learnedWord.setWid(word.getId());
            learnedWord.setLastSubmissionTime(timestamp);
            learnedWord.setLastSubmissionInterval(timestamp - getTime);
            learnedWord.setSource(word.getSource());
            if (count == null) {
                //还没学过
                count = 1;
                learnedWord.setCount(count);
                Boolean flag = wordDao.submitWordByUidAndWid(learnedWord);
                //提交成功
                return flag;
            } else {
                //学过了，更新操作，提交成功
                learnedWord.setCount(count+1);
                return wordDao.updateSubmitWord(learnedWord);
            }
        }
    }

    @Override
    //获取已经学过的数量和词库全部单词的数量
    public Scale getSacle(int id) {
        User user = userDao.getUserById(id);
        int num1 = 0;
        try {
            num1 = wordDao.getLearnCount(user);
        } catch (Exception e) {
            num1 = 0;
        }
        int num2 = wordDao.getAllWords(user).size();
        if (num1 == 0) {
            return new Scale(0, 1);
        } else {
            return new Scale(num1, num2);
        }
    }

    @Override
    //获取提交正确或错误的提示信息
    public List<Prompt> getPrompts() {
        return wordDao.getAllPrompts();
    }

    @Override
    //根据用户id和单词id查询当前单词错误率
    public double getErrorRateByUidAndWid(Integer uid, Integer wid) {
        UserLearnedWord word = wordDao.getUserLearnedWordByUidAndWid(1, 2345);
        double errorRate = (double) word.getErrorCount() / word.getSubmitCount();
        return errorRate;
    }
}
